#include "ItemTreeForm.h"
#include "ui_ItemTreeForm.h"

#include "d20.h"


#include <QMessageBox>
#include <QPushButton>
#include <QHeaderView>


using namespace d20;


namespace
{
enum ECol
{
    eCol_Name = 0,
    eCol_Button,
    eCol_Id
};
}



ItemTreeForm::ItemTreeForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ItemTreeForm)
{
    m_tree_buttons = 0;
    ui->setupUi(this);

    QHeaderView* header = ui->itemTreeWidget->header();
    header->setStretchLastSection(false);
    header->setResizeMode(eCol_Name, QHeaderView::Stretch);
    header->setResizeMode(eCol_Button, QHeaderView::ResizeToContents);
    onChanged();
}



ItemTreeForm::~ItemTreeForm()
{
    delete ui;
}



void ItemTreeForm::setCampaign(Campaign* c)
{
    disconnect(this, SLOT(onChanged()));
    m_campaign = c;

    onChanged(); // intentionally called before check
    if (m_campaign.isNull()) return;

    connect(m_campaign.data(), SIGNAL(sigChanged()), this, SLOT(onChanged()));
}


void ItemTreeForm::setCurrent(Item *item)
{
    if (!item) return;

    QTreeWidgetItem* top = ui->itemTreeWidget->topLevelItem(item->type().toInt());
    if (!top) return;

    for (int i = 0; i < top->childCount(); i++)
    {
        QTreeWidgetItem* sub = top->child(i);
        if (!sub) continue;
        if (item->id() != sub->data(eCol_Id, Qt::UserRole).toInt()) continue;

        ui->itemTreeWidget->setCurrentItem(sub);
        ui->itemTreeWidget->expandItem(top);
        return;
    }

}


void ItemTreeForm::onChanged()
{
    ui->itemTreeWidget->clear();
    if (m_tree_buttons) m_tree_buttons->deleteLater();
    m_tree_buttons = new QButtonGroup(this);
    connect(m_tree_buttons, SIGNAL(buttonClicked(int)), this, SLOT(onTreeButtonClicked(int)));

    for (int type = d20Type_FIRST; type <= d20Type_LAST; type++)
    {
        QTreeWidgetItem* item = new QTreeWidgetItem(ui->itemTreeWidget);
        item->setData(eCol_Name, Qt::DisplayRole, d20::typeToName(type));
        item->setData(eCol_Id, Qt::UserRole, type);
    }


    if (m_campaign.isNull()) return;

    QList<Item*> items = m_campaign->items();

    foreach (Item* item, items)
    {

        int type = item->type().toInt();
        if (type < d20Type_FIRST || type > d20Type_LAST) continue;


        QTreeWidgetItem* child = new QTreeWidgetItem(ui->itemTreeWidget->topLevelItem(type));
        child->setData(eCol_Name, Qt::DisplayRole, item->name());
        child->setData(eCol_Id, Qt::UserRole, item->id());

        QPushButton* btn = new QPushButton("X", this);
        btn->setMaximumSize(23,23);

        m_tree_buttons->addButton(btn, item->id().toInt());
        ui->itemTreeWidget->setItemWidget(child, eCol_Button, btn);

    }
}


void ItemTreeForm::onTreeButtonClicked(int id)
{
    m_campaign->removeItem(id);
}

void ItemTreeForm::on_itemTreeWidget_itemSelectionChanged()
{
    quint16 id = ui->itemTreeWidget->currentItem()->data(eCol_Id, Qt::UserRole).toUInt();

    Item* item = m_campaign->item(id);
    if (!item) return;

    emit sigItemSelected(item);

}
